package com.apestech.formwork.xml;

import com.apestech.formwork.xml.core.NodeBase;
import com.apestech.framework.json.JSON;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import org.dom4j.Attribute;
import org.dom4j.Element;

import java.util.ArrayList;
import java.util.List;

/**
 * Xml转Json格式
 *
 * @author hup
 * @create 2018-03-23-10:05
 */
public class XmlToJson {

    private Element node;
    private JSONObject out;

    public XmlToJson(Element node){
        this.node=node;
    }

    public void toJSON() throws Exception {
        toJSON(null);
    }

    public void toJSON(String[] listField) throws Exception {
        this.out=new JSONObject();
        JSONObject map=getNode(this.node, false, null, listField);
        System.out.println(JSON.toJSONString(this.out));
    }

    private JSONObject getNode(Element n, boolean blist, JSONObject inmap, String[] listField){
        NodeBase nb=new NodeBase(n);
        nb.getNodeInfo();
        JSONObject attr=nb.getAttr();
        ArrayList<Element> nlist=nb.getNodes();
        String nname=n.getName();
        if(inmap==null){
            inmap=new JSONObject();
        }
        if(attr==null){
            attr=new JSONObject();
        }
        String text=nb.getText();
        if(text!=null){
            attr.put("node_text",text);
        }
        if(nlist!=null){
            int len=nlist.size();
            JSONArray list=new JSONArray();
            for(int i=0; i<len; i++){
                String name=nlist.get(i).getName();
                //String[] xmlList=new String[]{"field","button","div","h1","sheet","label","page","group","form","tree","span"};  //odoo
                boolean flag=false;
                if(listField!=null){
                    flag=include(name, listField);
                }
                JSONObject map=getNode(nlist.get(i), flag, attr, listField);
                if(flag){
                    map.put("controlType", name);
                    list.add(map);
                }
            }
            if(list.size()>0){
                attr.put("list",list);
                //attr.put(nname,list);
            }
        }
        if(blist){
            return attr;
        } else {
            inmap.put(nname, attr);
            return inmap;
        }
    }

    public boolean include(String text, String[] list){
        boolean flag=false;
        int len=list.length;
        for(int i=0; i<len; i++){
            if(list[i].equals(text)){
                flag=true;
                break;
            }
        }
        return flag;
    }

    private void getNodes(Element n){
        System.out.println("--------------------");
        //当前节点的名称、文本内容和属性
        System.out.println("当前节点名称："+n.getName());//当前节点名称
        System.out.println("当前节点的内容："+n.getTextTrim());//当前节点名称
        List<Attribute> listAttr=n.attributes();//当前节点的所有属性的list
        for(Attribute attr:listAttr){//遍历当前节点的所有属性
            String name=attr.getName();//属性名称
            String value=attr.getValue();//属性的值
            System.out.println("属性名称："+name+"属性值："+value);
        }
        //递归遍历当前节点所有的子节点
        List<Element> listElement=n.elements();//所有一级子节点的list
        for(Element e:listElement){//遍历所有一级子节点
            this.getNodes(e);//递归
        }
    }




}
